import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:my_flutter_github/common/Config.dart';
import 'package:my_flutter_github/common/dao/ReposDao.dart';
import 'package:my_flutter_github/common/dao/UserEventDao.dart';
import 'package:my_flutter_github/model/UserEvent.dart';
import 'package:my_flutter_github/redux/MyState.dart';
import 'package:my_flutter_github/utils/UserEventUtils.dart';
import 'package:my_flutter_github/widgets/BaseListState.dart';
import 'package:my_flutter_github/widgets/PullLoadWidget.dart';
import 'package:my_flutter_github/widgets/UserEventItemWidget.dart';
import 'package:redux/redux.dart';

class DynamicTabPage extends StatefulWidget {
  @override
  _DynamicTabPageState createState() => _DynamicTabPageState();
}

class _DynamicTabPageState extends BaseListState<DynamicTabPage> with WidgetsBindingObserver {

  @override
  Future<Null> onRefresh() async {
    if (isLoading) {
      return null;
    }
    isLoading = true;
    pageIndex = 1;
    var result = await UserEventDao.getEventReceived(_getStore(), page: pageIndex, needDb: true);
    setState(() {
      pullLoadWidgetControl.needLoadMore = (result != null && result.length == Config.PAGE_SIZE);
    });
    isLoading = false;
    return null;
  }

  @override
  Future<Null> onLoadMore() async {
    if (isLoading) {
      return null;
    }
    isLoading = true;
    pageIndex++;
    var result = await UserEventDao.getEventReceived(_getStore(), page: pageIndex);
    setState(() {
      pullLoadWidgetControl.needLoadMore = (result != null);
    });
    isLoading = false;
    return null;
  }

  @override
  bool get autoRefresh => false;

  @override
  bool get wantKeepAlive => true;

  @override
  requestRefresh() {
  }

  @override
  requestLoadMore() {
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    ReposDao.getNewsVersion(context, false);
  }


  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    pullLoadWidgetControl.dataList = _getStore().state.eventList;
    if (pullLoadWidgetControl.dataList.length == 0) {
      showRefreshLoading();
    }
    super.didChangeDependencies();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      if (pullLoadWidgetControl.dataList.length != 0) {
        showRefreshLoading();
      }
    }
  }

  _renderEventItem(UserEvent e) {
    EventViewModel eventViewModel = EventViewModel.fromEventMap(e);
    return new UserEventItemWidget(
      eventViewModel,
      onPressed: () {
        UserEventUtils.ActionUtils(context, e, "");
      },
    );
  }

  Store<MyState> _getStore() {
    return StoreProvider.of(context);
  }

  @override
  Widget build(BuildContext context) {
    return new StoreBuilder<MyState>(
      builder: (context, store) {
        return PullLoadWidget(
          itemBuilder: (context, index) {
            return _renderEventItem(pullLoadWidgetControl.dataList[index]);
          },
          control: pullLoadWidgetControl,
          refreshCallback: onRefresh,
          loadMoreCallback: onLoadMore,
          refreshKey: refreshIndicatorKey,
        );
      },
    );
  }
}
